package com.ubird.astar.core;

import java.util.Arrays;

public class TiledMap {
	private final static int PATH = 0;
	private final static int BARRIER = 2;
	
	private int horizontalTilesNum, verticalTilesNum;
	private int[] mapData;
	
	public TiledMap(int hNum, int vNum) {
		this.horizontalTilesNum = hNum;
		this.verticalTilesNum = vNum;
		this.mapData = new int[vNum * hNum];
	}
	
	public void clear() {
		Arrays.fill(mapData, PATH);
	}

	public void loadData(int[][] data, int barrierVal, int clearVal) {
		horizontalTilesNum = data[0].length;
		verticalTilesNum = data.length;
		if (horizontalTilesNum * verticalTilesNum > mapData.length) {
			mapData = new int[horizontalTilesNum * verticalTilesNum];
		}

		clear();
		for (int i = 0; i < verticalTilesNum; i++) {
			for (int j = 0; j < horizontalTilesNum; j++) {
				if (data[i][j] == barrierVal) {
					set(j, i, BARRIER);
				} else {
					set(j, i, PATH);
				}
			}
		}
	}

	public int getHorizontalTilesNum() {
		return horizontalTilesNum;
	}

	public int getVerticalTilesNum() {
		return verticalTilesNum;
	}
	
	public int get(int x, int y) {
		return mapData[y * horizontalTilesNum + x];
	}
	
	public void set(int x, int y, int v) {
		mapData[y * horizontalTilesNum + x] = v;
	}
	
	public void setToBarrierNode(int x, int y) {
		set(x, y, BARRIER);
	}
	
	public void setToPathNode(int x, int y) {
		set(x, y, PATH);
	}
	
	public boolean isBarrier(int x, int y) {
		return get(x, y) == BARRIER;
	}
	
	public void toggleBarrier(int x, int y) {
		set(x, y, isBarrier(x, y) ? PATH : BARRIER);
	}
	
	public int[][] getData() {
		int[][] data = new int[verticalTilesNum][horizontalTilesNum];
		for (int y = 0; y < verticalTilesNum; ++y) {
			for (int x = 0; x < horizontalTilesNum; ++x) {
				data[y][x] = get(x, y);
			}
		}
		
		return data;
	}
}
